library(reshape2)
library(haven)
library(interflex)
library(dplyr)
library(plyr)
library(WriteXLS)
library(estimatr)
library(sjlabelled)

library(gplots)
library(ggplot2)
library(cowplot)
library(quanteda)
library(quanteda.textplots)


setwd("~/Dropbox/populism/jop/final_replication_material")



### Read in data

df<-read_dta("erosion.dta")
lapop<-read_dta("lapop.dta")

df<-remove_all_labels(df)
names(df)


### Results in main text

### Figure 1


## Predicted means

mod<-lm_robust(erosion~inparty+threatened+inparty_threatened+democracy+pid_intensity, data=df)

## Generate data to predict the model on

pred_data<-expand.grid(inparty=c(0,1), threatened=c(0,1))
pred_data$democracy<-mean(df$democracy, na.rm = T)
pred_data$pid_intensity<-mean(df$pid_intensity, na.rm = T)
pred_data$inparty_threatened<-pred_data$inparty*pred_data$threatened

preds<-predict(mod, newdata = pred_data, se=T)

pred_data$fit<-preds$fit
pred_data$fit.se<-preds$se.fit

pred_data$threat_lab[pred_data$threatened==1]<-"Threat prime"
pred_data$threat_lab[pred_data$threatened==0]<-"No threat prime"

pred_data$scenario_lab[pred_data$inparty==1]<-"In-party"
pred_data$scenario_lab[pred_data$inparty==0]<-"Out-party"

### FIGURE 1

bar<-ggplot(pred_data, aes(x = scenario_lab, y = fit,fill = scenario_lab)) +
    geom_bar(stat = "identity") + facet_wrap(.~threat_lab) +
   ggtitle("(A) Democratic hypocrisy and threat") + ylab("Eroding attitudes") +xlab("Party in power")+
   geom_errorbar(aes(ymin = fit - 1.96*fit.se, ymax = fit + 1.96*fit.se), width = 0.1)+
  scale_fill_grey(start=0.8, end=0.4)+ theme_bw()+
  theme(legend.position = "none",plot.title = element_text(size=12)) 


ifle<-interflex(estimator="kernel",subset(df, threatened==0), "erosion","inparty" 
                   ,"pid_intensity", 
                   na.rm=T, treat.type = "discrete",
                   theme.bw = T, show.grid = F, Xdistr="hist")

flex<-plot(ifle,theme.bw = T, show.grid = F, Xdistr="hist",
     xlab="Expressive partisanship",
     ylab="Effect of In-party vs. Out-party",
     main="(B) Democratic hypocrisy and expressive partisanship ",
     cex.main = .7 , cex.sub = NULL, cex.lab = .7, cex.axis = .67)



plot_grid(bar, flex)

### Figure 2

#### Government analysis

governor<-ddply(df, .( pid3, demgov), summarise,
                   erosion.mean = mean(erosion_c_gov, na.rm=T),
                   erosion.se = sd(erosion_c_gov, na.rm=T)/sqrt(length(erosion_c_gov)))

governor<-subset(governor, pid3 %in% c(-1, 1))
governor<-subset(governor, demgov %in% c(0,1))

governor$governor<-factor(governor$demgov,
                         levels = c(0,1),
                         labels = c("Republican", "Democrat"))

governor$party<-factor(governor$pid3,
                     levels = c(-1,1),
                     labels = c( "Republican respondents","Democrat respondents"))


bar.gov<-ggplot(governor, aes(x = governor, y = erosion.mean,fill = governor)) +
  geom_bar(stat = "identity") + facet_wrap(.~party) +
  ggtitle("(A) Support for erosion across states") + ylab("Eroding attitudes") +xlab("Governor's party")+
  geom_errorbar(aes(ymin = erosion.mean - 1.96*erosion.se, ymax = erosion.mean + 1.96*erosion.se), width = 0.1)+
  scale_fill_grey(start=0.8, end=0.4)+ theme_bw()+
  theme(legend.position = "none",plot.title = element_text(size=12)) 

#### Lapop analysis

lapops<-ddply(lapop, .( pid, republican_president), summarise,
                erosion.mean = mean(dissolve, na.rm=T),
                erosion.se = sd(dissolve, na.rm=T)/sqrt(length(dissolve)))
lapops<-subset(lapops, pid %in% c(-1, 1))

lapops$president<-factor(1-lapops$republican_president,
                         levels = c(0,1),
                         labels = c( "Repubican", "Democrat"))

lapops$party<-factor(lapops$pid,
                         levels = c(-1,1),
                         labels = c( "Republican respondents","Democrat respondents"))


bar.lapop<-ggplot(lapops, aes(x =president, y = erosion.mean,fill = president)) +
  geom_bar(stat = "identity") + facet_wrap(.~party) +
  ggtitle("(B) Support for erosion across administrations") + ylab("Supports dissolvong Congress in crisis") +xlab("Presidents's party")+
  geom_errorbar(aes(ymin = erosion.mean - 1.96*erosion.se, ymax = erosion.mean + 1.96*erosion.se), width = 0.1)+
  scale_fill_grey(start=0.8, end=0.4)+ theme_bw()+
  theme(legend.position = "none",plot.title = element_text(size=12)) 



plot_grid( bar.gov,bar.lapop)


##### Figure OA1-2: Word clouds

XXXXX


threattext <- corpus(df$Q98)

docvars(threattext, "party")<-df$respondent_party

dtm_threat <- dfm(threattext,remove = stopwords("english"), stem = F, verbose = TRUE,remove_punct=TRUE)
dtm_threat_slim<-dfm_trim(dtm_threat, min_termfreq = 10,max_termfreq = 2000)



dtm_threat_party <- dfm_group(dtm_threat_slim, groups = df$respondent_party)


gops<-dfm_subset(dtm_threat_party, party=="Republican")
dems<-dfm_subset(dtm_threat_party, party=="Democrat")

par(mar=c(0,0,0,0),oma=c(0,0,0,0), mfrow=c(1,1))
textplot_wordcloud(dems, color = "blue",max_words = 30)
textplot_wordcloud(gops,color = "red",max_words = 30)

##### #### Figure OA3: Norm violation by partisan groups



df_control<-subset(df, erosion_condition=="control" ,select = c("COURT_c", "BANPROTS_c", "PROSJOUR_c",
                                "OBEYCOUR_c", "DISQCRIT_c","respondent_party", "pid3", "ResponseId"))

table(df_control$pid3)


df_control_long<-melt(df_control, id.vars = c("respondent_party", "pid3", "ResponseId"),
                      variable.name="scenario"	)

summary_all<-ddply(df_control_long, .( pid3), summarise,
                   erosion.mean = 100*mean(value, na.rm=T),
                   erosion.se = 100*sd(value, na.rm=T)/sqrt(length(value)))
summary_all$scenario<-"Total"

summary<-ddply(df_control_long, .(pid3, scenario), summarise,
            erosion.mean = 100*mean(value, na.rm=T),
            erosion.se = 100*sd(value, na.rm=T)/sqrt(length(value)))

summary<-rbind(summary, summary_all)

summary$party<-factor(summary$pid3, levels = c(-1,0,1), labels =c("GOP","Ind","Dem") )


summary$scenlab[summary$scenario=="COURT_c"]<-"Pack Supreme Court"
summary$scenlab[summary$scenario=="BANPROTS_c"]<-"Ban protests"
summary$scenlab[summary$scenario=="DISQCRIT_c"]<-"Disqualify candidates"
summary$scenlab[summary$scenario=="OBEYCOUR_c"]<-"Disregard biased courts"
summary$scenlab[summary$scenario=="PROSJOUR_c"]<-"Prosecute journalists"
summary$scenlab[summary$scenario=="Total"]<-"Total"

bar.control<-ggplot(summary, aes(x = party, y = erosion.mean,fill = party)) +
  geom_bar(stat = "identity") + facet_wrap(.~scenlab,  ncol = 3)+
  ggtitle("") + ylab("Eroding attitudes") +xlab("")+
  geom_errorbar(aes(ymin = erosion.mean - 1.96*erosion.se, ymax = erosion.mean + 1.96*erosion.se), width = 0.1)+
  scale_fill_grey(start=0.9, end=0.4)+ theme_bw()+
  theme(legend.position = "none",plot.title = element_text(size=12))


bar.control

